package com.artisan.provider.controller


import org.jsoup.Connection
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.jsoup.select.Elements
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class JsoupController {


    /**
     * 爬取亚马逊国外站香烟类目的数据
     * @return
     */
    @GetMapping(value = "/api/jsoup/findAll")
    public List<com.artisan.provider.jsoup.JsoupList> jsoupFindAll(){

        List<com.artisan.provider.jsoup.JsoupList> jsoupLists = new ArrayList<>();
        String http = "https://www.amazon.com/s?k=cigarette&__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&ref=nb_sb_noss_1";
        try {

            Document document = Jsoup.connect(http).get();

            Elements elementsByClass = document.getElementsByClass("s-main-slot");
            File file = new File("E:/jsoupimg/uuidName.txt");
            for (Element byClass : elementsByClass) {
                Elements elementsByClass1 = byClass.getElementsByClass("sg-col-4-of-12");
                for (Element element : elementsByClass1) {
                    Elements elementsByClass2 = element.getElementsByClass("a-spacing-medium");

                    //第一层获取image
                    for (Element element1 : elementsByClass2) {
                        Elements elementsByClass3 = element1.getElementsByClass("s-image-square-aspect");
                        for (Element element2 : elementsByClass3) {
//                            Elements img = element2.getElementsByTag("img");
//                            String src = img.attr("src").trim();
//                            //System.out.println(img); //拿到封面图
//                            CloseableHttpClient httpClient = HttpClients.createDefault();
//                            HttpGet httpGet = new HttpGet(src);
//                            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
//                            HttpEntity entity = httpResponse.getEntity();
//                            InputStream is = entity.getContent();
//                            String imgName = ""+UUID.randomUUID().toString().replaceAll("-", "") + src.substring(src.lastIndexOf("."));
//                            try {
//                                Thread.sleep(1000);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                            FileOutputStream out1 = new FileOutputStream("E:/jsoupimg/jsoupName/" + imgName);
//                            FileOutputStream out = new FileOutputStream(file);
//
//                            out.write(imgName.getBytes());
//                            //IOUtils.copy(is, out);
//                            IOUtils.copy(is, out1);
//                            is.close();
//                            out1.close();
                        }
                    }


                    //第二层第一子层,描述层
                    for (Element element1 : elementsByClass2) {
                        com.artisan.provider.jsoup.JsoupList jsoupList = new com.artisan.provider.jsoup.JsoupList();

                        String img = element1.getElementsByTag("img").attr("src").trim();
                        //System.out.println(img);
                        String imgName = ""+UUID.randomUUID().toString().replaceAll("-", "") + img.substring(img.lastIndexOf("."));
                        jsoupList.setImage(imgName);

                        Elements elementsByClass3 = element1.getElementsByClass("a-size-base-plus");
                        //System.out.println(elementsByClass3.text()); //拿到描述
                        jsoupList.setTitle(elementsByClass3.text());

                        Elements elementsByClass4 = element1.getElementsByClass("a-color-information");
                        //System.out.println(elementsByClass4.text()); //这个是部分有 Pack of


                        Elements elementsByClass5 = element1.getElementsByClass("a-icon-alt");
                        //System.out.println(elementsByClass5.text()); //部分无评分 拿到评分

                        Elements elementsByClass6 = element1.getElementsByClass("a-offscreen");
                        //System.out.println(elementsByClass6.text()); //拿到商品的价格 price 美元
                        jsoupList.setPrice(elementsByClass6.text());

                        Elements elementsByClass7 = element1.getElementsByClass("s-align-children-center");
                        //Element span = element1.getElementsByTag("span").last();

//                        for (Element element2 : elementsByClass7) {
//                            String span = element2.getElementsByTag("span").text();
//                            System.out.println(span);  //发往中国
//                        }

                        System.out.println("----------------------------------------------------------------------------");
                        jsoupLists.add(jsoupList);
                    }


                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }



        return jsoupLists;
    }

    /**
     * 爬取乐天百货电子眼栏目所有商品
     * @return
     */
    @GetMapping(value = "/api/jsoup/lt/findAll")
    public List<com.artisan.provider.jsoup.JsoupList> jsoupLtFindAll(){
        String http = "Document document = Jsoup.connect(http).get();";
        Document document = Jsoup.connect(http).get();

        return null;
    }

    /**
     * 爬取亚马逊中国
     * @return
     */
    @GetMapping(value = "/api/jsoup/china/ynx/findAll")
    public List<com.artisan.provider.jsoup.JsoupList> jsoupLtFindChinaAll(){
        String http = "https://www.amazon.cn/b?node=2031274071&pf_rd_r=GMG9TJ60HA95CYBJBT2D&pf_rd_p=1bf0d694-f0aa-43ed-b092-d00e6994858f&pd_rd_r=3ec84c8f-5d39-4d39-889d-7715ce0bfd82&pd_rd_w=0G0cV&pd_rd_wg=RkuwS&ref_=pd_gw_unk";
        Document document = Jsoup.connect(http).get();
        Elements searchresults = document.getElementsByClass("a-column");
        System.out.println(searchresults)
        return null;
    }

    @GetMapping(value = "/api/jsoup/china/sm/findAll")
    public String jsoupChinaSmFindAll(){

        return null;
    }

    @GetMapping(value = "/api/jsoup/facebook")
    public String jsoupFaceBook(){

        Connection.Response res = Jsoup.connect("https://www.facebook.com/login.php")
                .data("email", "100070695925043", "pass", "@K9a9#9mito")
                .method(Method.POST)
                .execute();

        System.out.println(res.statusCode());
        Document doc = res.parse();
        String sessionId = res.cookie("SESSIONID");

        println res.body()
        return null;
    }


}
